import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileView;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.JComboBox;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;


public class TokenBoardSettings extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private IconResizer resizer = new IconResizer(null);


	/**
	 * Create the dialog.
	 */
	public TokenBoardSettings(final TokenBoard tb) {
		setResizable(false);
		setBounds(100, 100, 295, 170);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblTokens = new JLabel("Tokens:");
		lblTokens.setBounds(10, 11, 46, 14);
		contentPanel.add(lblTokens);
		
		final JComboBox comboBox = new JComboBox();
		
		for (int i = 0; i < tb.getNoOfTokens(); i++){
			Token token = tb.getToken(i);
			String filePath = token.getImage();
			String fileName = filePath.substring(12);
			comboBox.addItem(fileName.substring(0, fileName.length() - 4));
		}
		
		comboBox.setBounds(10, 27, 142, 22);
		contentPanel.add(comboBox);
		
		final JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(163, 11, 116, 92);
		lblNewLabel.setBorder(new LineBorder(Color.BLACK));
		contentPanel.add(lblNewLabel);
		
		if (comboBox.getItemCount() > 0){
			resizer.setImage(new ImageIcon("src/gallery/" + comboBox.getSelectedItem() + ".jpg"));
			resizer.resize(92, 116);
			lblNewLabel.setIcon(resizer.getImage());
		}
		
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if (comboBox.getItemCount() > 0){
					resizer.setImage(new ImageIcon("src/gallery/" + comboBox.getSelectedItem() + ".jpg"));
					resizer.resize(92, 116);
					lblNewLabel.setIcon(resizer.getImage());
				} else {
					lblNewLabel.setIcon(null);
				}
			}
		});
		
		final JButton btnNewButton = new JButton("Add Token");
		final JButton btnRemoveSelectedToken = new JButton("Remove Selected Token");
		
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser insertImage = new JFileChooser ();
				FileNameExtensionFilter fileFilter = new FileNameExtensionFilter("Image Files", "jpg", "jpeg", "bmp", "png", "gif");
				insertImage.setAcceptAllFileFilterUsed(false);
				insertImage.setFileFilter(fileFilter);
				insertImage.setCurrentDirectory(new File ("src/gallery/"));
				int dialogReturn = insertImage.showDialog(getParent(), "Insert");
				if (dialogReturn == JFileChooser.APPROVE_OPTION){
					File file = insertImage.getSelectedFile();
					try {
						File output = new File ("src/gallery/" + file.getName());
						RenderedImage image = ImageIO.read(file);
						ImageIO.write(image, "jpg", output);
						String fileName = output.getName();
						comboBox.addItem(fileName.substring(0, fileName.length() - 4));
						comboBox.setSelectedIndex(comboBox.getItemCount() - 1);
					} catch (IOException ex) {
						// TODO Auto-generated catch block
						ex.printStackTrace();
					}
					if (comboBox.getItemCount() >= 4){
						btnNewButton.setEnabled(false);
					} else {
						btnNewButton.setEnabled(true);
					}
					btnRemoveSelectedToken.setEnabled(true);
				}
			}
		});
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 8));
		btnNewButton.setBounds(10, 53, 142, 23);
		contentPanel.add(btnNewButton);
		
		if (comboBox.getItemCount() >= 4){
			btnNewButton.setEnabled(false);
		}
		
		
		btnRemoveSelectedToken.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				comboBox.removeItemAt(comboBox.getSelectedIndex());
				if (comboBox.getItemCount() <= 0){
					btnRemoveSelectedToken.setEnabled(false);
				} else {
					btnRemoveSelectedToken.setEnabled(true);
				}
				btnNewButton.setEnabled(true);
			}
		});
		btnRemoveSelectedToken.setFont(new Font("Sylfaen", Font.PLAIN, 8));
		btnRemoveSelectedToken.setBounds(10, 80, 142, 23);
		contentPanel.add(btnRemoveSelectedToken);
		
		if (comboBox.getItemCount() <= 0){
			btnRemoveSelectedToken.setEnabled(false);
		}

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						tb.clearTokens();
						for (int i = 0; i < comboBox.getItemCount(); i++){
							tb.setToken("src/gallery/" + comboBox.getItemAt(i) + ".jpg");
						}
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
}
